//
//  LDClassCheckAll.m
//  Gift
//
//  Created by 李迪 on 15/11/14.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "LDClassCheckAll.h"
#import "BaseHeader.h"
#import "LDCheckCell.h"
#import "LDSpecialTabView.h"

@interface LDClassCheckAll ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)UITableView *tab;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, assign)NSInteger page;



@end

@implementation LDClassCheckAll

-(void)dealloc{
    
    self.tab.delegate = nil;
    self.tab.dataSource = nil;
    [self.tab release];
    [self.array release];
    [super dealloc];
}


- (void)createTab{
    
    self.tab = [[UITableView alloc]initWithFrame:self.view.bounds];
    self.tab.delegate = self;
    self.tab.dataSource = self;
    self.tab.rowHeight = SCREEN_HEIGHT / 4;
    [self.tab registerClass:[LDCheckCell class] forCellReuseIdentifier:@"LDClassCheckAllCell"];
    [self.view addSubview:self.tab];
    [self.tab release];
}

#pragma mark - 数据解析
- (void)handleDataNumber:(NSInteger)number{
    
    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections?limit=20&offset=%ld",number];
    [PFAFNetworking getUrl:str body:nil response:PFJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        if (number == 0) {
            self.array = [[NSMutableArray array]init];
        }
        for (NSDictionary *dic in [[resposeObject objectForKey:@"data"]objectForKey:@"collections"]) {
            ClassificationModel *model = [ClassificationModel modelSetDic:dic];
            [self.array addObject: model];
        }
        
        [self.tab reloadData];
        [self.tab.header endRefreshing];
        [self.tab.footer endRefreshing];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
}

- (void)addHarder{
    
    self.tab.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self handleDataNumber:self.page];
    }];
    
    [self.tab.header beginRefreshing];
}

- (void)addFoot{
    
    self.tab.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.page = self.page + 20;
        [self handleDataNumber:self.page];
    }];
    [self.tab.footer beginRefreshing];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LDCheckCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LDClassCheckAllCell"];
    ClassificationModel *model = [self.array objectAtIndex:indexPath.row];
    cell.model = model;

    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ClassificationModel *model = [self.array objectAtIndex:indexPath.row];
    LDSpecialTabView *special = [[LDSpecialTabView alloc]init];
    special.number = model.Cid;
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:special animated:YES];
    [special release];
}



-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTab];
    [self addHarder];
    [self addFoot];
    self.title = @"全部精选";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.], NSForegroundColorAttributeName:[UIColor colorWithRed:1.000 green:1.00 blue:1.000 alpha:1.000]}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
